package ribbitmq_3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer_3 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory c = new ConnectionFactory();
        c.setHost("192.168.64.140");
        c.setPort(5672);
        c.setUsername("admin");
        c.setPassword("admin");

        Channel ch = c.newConnection().createChannel();
        //定义随机队列名
        String uuid = UUID.randomUUID().toString();
        ch.queueDeclare(uuid,
                false,
                true,
                true,
                null);
        //定义交换机
        ch.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        //绑定交换机
        ch.queueBind(uuid, "logs", "");

        //正常消费
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] bytes = message.getBody();
                String msg = new String(bytes);
                System.out.println("以收到消息" + msg);
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };

        ch.basicConsume(uuid, true, deliverCallback, cancelCallback);

    }

}
